package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.ErrorCodes;
import unipi.database.Rights;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Table;

public class EditRights extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public EditRights()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String[] message)
	{
		String content = "<h1>Επεξεργασία Δικαιωμάτων</h1>";
		
		Map<String, Boolean> rights = Rights.getRights(uid);
		
		if(!rights.get("userRights"))
		{
			content += "Δεν έχετε δικαίωμα αφαίρεσης δικαιωμάτων.";			
		}
		else
		{
			List<List<String>> usersPerDpt = Rights.getEditRightsPerDpt(uid);
			List<List<String>> usersPerUser = Rights.getEditRightsPerUser(uid);
			
			if(usersPerDpt != null || usersPerUser != null)
			{
				content += "<h2>ανά τμήμα</h2>";
				
				if(usersPerDpt.size() == 0)
				{
					content += "Δεν υπάρχουν δικαιώματα προς επεξεργασία.";
				}
				else
				{							
					List<String> coltitles = Arrays.asList(new String[]{"Όνομα Χρήστη", "στο Τμήμα" ,"Δικαίωματα"});
					
					Table t = new Table(coltitles);
					
					boolean even = false;
					for(List<String> row : usersPerDpt)
					{
						t.addRow(row, even);
						even = !even;
					}
					content += t.getHTML();
					
					content += message[0];
				}
				
				content += "<h2>ανά χρήστη</h2>";
				
				if(usersPerUser.size() == 0)
				{
					content += "Δεν υπάρχουν δικαιώματα προς επεξεργασία.";
				}
				else
				{						
					List<String> coltitles = Arrays.asList(new String[]{"Ο χρήστης", "έχει δικαίωμα εργασίας στον χρήστη" ,""});
					
					Table t = new Table(coltitles);
					
					boolean even = false;
					for(List<String> row : usersPerUser)
					{
						t.addRow(row, even);
						even = !even;
					}
					content += t.getHTML();
					
					content += message[1];
				}
			}
			else
			{				
				content += "Παρουσιάστηκε πρόβλημα με την φόρτωση των χρηστών.";
			}
		}		
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String rid = request.getParameter("rid");
			String urid = request.getParameter("urid");
			String message[] = {"", ""};
			
			if(rid != null)
			{
				boolean taskRights = request.getParameter("taskRights") != null;
				boolean userRights = request.getParameter("userRights") != null;
				boolean dptRights = request.getParameter("dptRights") != null;	
				
				message[0] = "Τα δικαιώματα επεξεργάστηκαν με επιτυχία.";
				ErrorCodes er = Rights.editRights(rid, taskRights, userRights, dptRights);
				if(er != ErrorCodes.OK)
				{
					message[0] = "Παρουσιάστηκε σφάλμα κατα την ενημέρωση των δικαιωμάτων.";
				}			
			}
			else if(urid != null)
			{
				String parentUser = request.getParameter("uridP");
				String childUser = request.getParameter("uridC");
				
				message[1] = "Το δικαίωμα αφαιρέθηκε με επιτυχία.";
				ErrorCodes er = Rights.removeUserRights(parentUser, childUser);
				if(er != ErrorCodes.OK)
				{
					message[1] = "Παρουσιάστηκε σφάλμα κατα την ενημέρωση των δικαιωμάτων.";
				}	
				
			}
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, message));
			
			out.close();			
		}

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, new String[]{"", ""}));
			
			out.close();			
		}
	}
	
}
